package 蓝桥算法;

public class Node2 {
    Node head;
    static class Node{
        int value;
        Node next;
        public Node(int value, Node next){
            this.next=next;
            this.value=value;
        }
    }
    public void addFirst(int value){
        head=new Node(value,head);                 //头指针不存储数据
    }
    public void loop1(){
        Node p=head;
        while (p!=null){
            System.out.println(p.value);
            p=p.next;
        }
    }
    public Node reserveList(Node head){
        Node pre=null;
        Node cur=head;
        Node temp=null;
        while(cur!=null){
            temp=cur.next;
            cur.next=pre;
            pre=cur;
            cur=temp;//往后移
        }
        return pre;//头节点
    }
    public Node reserveList1(Node head){
        return reserve(null,head);
    }
    public Node reserve(Node pre,Node cur){
        if(cur==null){
            return pre;
        }
        Node temp=null;
        temp=cur.next;//记录下一个值
        cur.next=pre;
        reserve(cur,temp);//进行下一轮反转
        return pre;
    }
    public Node change(Node head){
       Node pre=new Node(-1,null);
        pre.next=head;
        Node cur=pre;
        Node temp=null;//保存两个节点后面的节点
        Node FirstNode=null;//两个节点的前一个
        Node SecondNode=null;//两个节点的后一个
        while(cur.next!=null&&cur.next.next!=null){
           temp=cur.next.next.next;
           FirstNode=cur.next;
           SecondNode=cur.next.next;
           cur.next=SecondNode;
           SecondNode=FirstNode;
           FirstNode=temp;
           cur=FirstNode;
        }
        return pre.next;
    }
}
